package com.chonor.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.chonor.mall.dao.PortalProductDao;
import com.chonor.mall.domain.CartProduct;
import com.chonor.mall.domain.CartPromotionItem;
import com.chonor.mall.mapper.CartItemMapper;
import com.chonor.mall.mapper.ProductMapper;
import com.chonor.mall.model.CartItem;
import com.chonor.mall.model.CartItemExample;
import com.chonor.mall.model.Member;
import com.chonor.mall.model.Product;
import com.chonor.mall.service.CartItemService;
import com.chonor.mall.service.MemberService;
import com.chonor.mall.service.PromotionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车管理Service实现类
 */
@Service
public class CartItemServiceImpl implements CartItemService {
    @Autowired
    private CartItemMapper cartItemMapper;
    @Autowired
    private PortalProductDao productDao;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private PromotionService promotionService;
    @Autowired
    private MemberService memberService;

    @Override
    public int add(CartItem cartItem) {
        int count;
        Member currentMember =memberService.getCurrentMember();
        cartItem.setMemberId(currentMember.getId());
        cartItem.setMemberNickname(currentMember.getNickname());
        cartItem.setIsdelete(0);
        CartItem existCartItem = getCartItem(cartItem);
        //判断商品是否已存在购物车中
        if (existCartItem == null) {
            cartItem.setCreateat(new Date());
            count = cartItemMapper.insert(cartItem);
        } else {
            cartItem.setUpdateat(new Date());
            existCartItem.setQuantity(existCartItem.getQuantity() + cartItem.getQuantity());
            count = cartItemMapper.updateByPrimaryKey(existCartItem);
        }
        return count;
    }

    /**
     * 根据会员id,商品id和规格获取购物车中商品
     */
    private CartItem getCartItem(CartItem cartItem) {
        CartItemExample example = new CartItemExample();
        CartItemExample.Criteria criteria = example.createCriteria().andMemberIdEqualTo(cartItem.getMemberId())
                .andProductIdEqualTo(cartItem.getProductId()).andIsdeleteEqualTo(0);
        if (!StringUtils.isEmpty(cartItem.getProductSkuId())) {
            criteria.andProductSkuIdEqualTo(cartItem.getProductSkuId());
        }
        List<CartItem> cartItemList = cartItemMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(cartItemList)) {
            return cartItemList.get(0);
        }
        return null;
    }

    @Override
    public List<CartItem> list(Long memberId) {
        CartItemExample example = new CartItemExample();
        example.createCriteria().andIsdeleteEqualTo(0).andMemberIdEqualTo(memberId);
        List<CartItem> cartItems = cartItemMapper.selectByExample(example);
        if(cartItems.size() > 0){
            for (CartItem cartItem : cartItems) {
                convert(cartItem);
            }
        }
        return cartItems;
    }

    @Override
    public List<CartItem> list(Long memberId, List<Long> cartIds) {
        //获取会员购物车列表
        List<CartItem> cartItemList = list(memberId);
        //筛选出选择的商品
        if(CollUtil.isNotEmpty(cartIds)){
            cartItemList = cartItemList.stream().filter(item->cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        return cartItemList;
    }

    private void convert(CartItem cartItem){
        Product product = productMapper.selectByPrimaryKey(cartItem.getProductId());
//        BeanUtils.copyProperties(product,cartItem);
        cartItem.setPrice(product.getPrice());
        cartItem.setProductPic(product.getPic());
        cartItem.setProductName(product.getName());
    }

    @Override
    public List<CartPromotionItem> listPromotion(Long memberId, List<Long> cartIds) {
        //获取会员购物车列表
        List<CartItem> cartItemList = list(memberId);
        //筛选出选择的商品
        if(CollUtil.isNotEmpty(cartIds)){
            cartItemList = cartItemList.stream().filter(item->cartIds.contains(item.getId())).collect(Collectors.toList());
        }
        List<CartPromotionItem> cartPromotionItemList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(cartItemList)){
            cartPromotionItemList = promotionService.calcCartPromotion(cartItemList);
        }
        return cartPromotionItemList;
    }

    @Override
    public int updateQuantity(Long id, Long memberId, Integer quantity) {
        CartItem cartItem = new CartItem();
        cartItem.setQuantity(quantity);
        CartItemExample example = new CartItemExample();
        example.createCriteria().andIsdeleteEqualTo(0)
                .andIdEqualTo(id).andMemberIdEqualTo(memberId);
        return cartItemMapper.updateByExampleSelective(cartItem, example);
    }

    @Override
    public int delete(Long memberId, List<Long> ids) {
        CartItem record = new CartItem();
        record.setIsdelete(1);
        CartItemExample example = new CartItemExample();
        example.createCriteria().andIdIn(ids).andMemberIdEqualTo(memberId);
        int count = cartItemMapper.updateByExampleSelective(record, example);
        return count;
    }

    @Override
    public CartProduct getCartProduct(Long productId) {
        return productDao.getCartProduct(productId);
    }

    @Override
    public int updateAttr(CartItem cartItem) {
        //删除原购物车信息
        CartItem updateCart = new CartItem();
        updateCart.setId(cartItem.getId());
        updateCart.setUpdateat(new Date());
        updateCart.setIsdelete(1);
        cartItemMapper.updateByPrimaryKeySelective(updateCart);
        cartItem.setId(null);
        add(cartItem);
        return 1;
    }

    @Override
    public int clear(Long memberId) {
        CartItem record = new CartItem();
        record.setIsdelete(1);
        CartItemExample example = new CartItemExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        return cartItemMapper.updateByExampleSelective(record,example);
    }
}
